"""支付宝支付
"""
from alipay.aop.api.AlipayClientConfig import AlipayClientConfig
from alipay.aop.api.DefaultAlipayClient import DefaultAlipayClient
from alipay.aop.api.domain.AlipayTradePagePayModel import AlipayTradePagePayModel
from alipay.aop.api.request.AlipayTradePagePayRequest import AlipayTradePagePayRequest
import time
import random

class OrderUtil():
    
    @staticmethod
    def order_id():
        """创建订单号

        Returns:
            _type_: _description_
        """
        return str(time.time()).replace(".","") + str(random.randint(10000000,99999999))
    
    @staticmethod
    def get_web_callback_url(_type):
        """获取网站支付的回调URL

        Returns:
            _type_: _description_
        """
        if _type == "vip":
            return "http://itedu.smartopen.cn/itedu/v1/vip/pay/"
        else:
            return "http://itedu.smartopen.cn/itedu/v1/video/pay/"


class AliPayUtil():
    
    """PC网站支付
    """
    @staticmethod
    def ali_web_pay(title,price,order_id,callback_url,return_url="none"):
        
        alipay_client_config = AlipayClientConfig()
        
        alipay_client_config.server_url = "https://openapi.alipay.com/gateway.do"
        alipay_client_config.app_id = WEB_APP_ID
        alipay_client_config.app_private_key = WEB_APP_PRIVATE_KEY
        alipay_client_config.alipay_public_key = WEB_ALIPAY_PUBLIC_KEY
        alipay_client_config._sign_type = "RSA2"

        client = DefaultAlipayClient(alipay_client_config,None)
        
        model = AlipayTradePagePayModel()
        
        model.out_trade_no = order_id
        model.total_amount = price
        model.subject = title 
        model.product_code="FAST_INSTANT_TRADE_PAY"
        model.qr_pay_mode="4"
        model.qrcode_width="300"
        model.passback_params=order_id
        model.integration_type="PCWEB"

        request = AlipayTradePagePayRequest(biz_model=model)
        
        request.return_url = return_url 
        request.notify_url = callback_url 
        
        response = client.page_execute(request,http_method="GET")

        return response